package com.qfedu.repo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.qfedu.repo.bean.Product;
import com.qfedu.repo.bean.RespResult;
import com.qfedu.repo.common.Const;
import com.qfedu.repo.common.StreamUtils;
import com.qfedu.repo.dao.ProductDao;
import com.qfedu.repo.dao.impl.ProductDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * projectName:QfeduJava2401
 * author:jww
 * time:2024/5/9 10:04
 * description:
 */
@WebServlet("/product")
public class ProductServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding(Const.CHARACTOR_ENCODING);

        String method = request.getParameter("method");
        List<Product> products = null;
        RespResult result = new RespResult();
        switch (method) {
            case "allList":
                products = allList();
                //专门的获取数据列表的功能代码
                if (products == null || products.isEmpty()) {
                    result.setMsg("未获取到列表数据");
                    result.setCode(Const.PRODUCT_EMPTY);
                } else {
                    result.setMsg("获取列表成功");
                    result.setCode(Const.PRODUCT_SUCCESS);
                    result.setRepo(products);
                }
                break;
            default:
                break;
        }

        //返回前端
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(result));
        writer.flush();
        writer.close();
    }

//    private int addProduct(HttpServletRequest request) {
//        //1.从request请求对象中获取传参
////        String pno = request.getParameter("pno");
////        String pname = request.getParameter("pname");
////        String paddr = request.getParameter("paddr");
////        String pdate = request.getParameter("pdate");
////        String enterdate = request.getParameter("enterdate");
////        int pnumber = Integer.parseInt(request.getParameter("pnumber"));
//        //2.根据参数构建新增的Product对象
////        Product prod=new Product(-1,pno,pname,paddr,pdate,enterdate,pnumber);
////        //3.调用数据库执行
////        ProductDao dao=new ProductDaoImpl();
////        return dao.addProduct(prod);
//
//    }

    public List<Product> allList() {
        ProductDao productDao = new ProductDaoImpl();
        return productDao.getAllList();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        this.doGet(request, response);
        response.setCharacterEncoding(Const.CHARACTOR_ENCODING);

        BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));

        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line); // 逐行读取并添加到StringBuilder中
        }

        StreamUtils.readStreamData(request.getInputStream());
        //获取到底前端传输的数据：json格式字符串
        //'{"pno":10010,"pname":"蛋黄派"...}'
        String reqData = StreamUtils.readStreamData(request.getInputStream());

        //反序列化
        Product product = JSON.parseObject(reqData, Product.class);
        System.out.println(product.getPname());
        System.out.println(product.getPaddr());
        ProductDao dao = new ProductDaoImpl();
        int flag = dao.addProduct(product);
        RespResult result=new RespResult();
        if (flag == 1) {
            result.setMsg("产品入库成功");
            result.setCode(Const.PRODUCT_ENTER_SUCCESS);
        } else {
            result.setMsg("产品入库失败");
            result.setCode(Const.PRODUCT_ENTER_FAILED);
        }

        //返回前端
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(result));
        writer.flush();
        writer.close();
    }
}


